/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package beanUser;

import com.matrimonysite.entity.Users;
import com.matrimonysite.sessionbean.UsersFacade;
import helper.MD5;
import helper.messages;
import java.util.Date;
import javax.ejb.EJB;
import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author khanhnguyen
 */
//@ManagedBean(name = "AddUsers")
@ManagedBean (name = "Reg")
@RequestScoped
public class Register {

    @EJB
    private UsersFacade usersFacade;
    private Users users;
    

    /**
     * Creates a new instance of Register
     */
    public Register() {
        
    }
    private String user;
    private String pass;
    private String email;
    private Date createDate;
    private Date lastLogin;
    private int userStatus;
    private int roleID;
    public void createUser(String role) {
        try{
        if(user.length() == 0)
        {
         messages.taoTB(FacesMessage.SEVERITY_ERROR, "User Not null", "Error User");
        }else if(pass.length() ==0)
        {
          messages.taoTB(FacesMessage.SEVERITY_ERROR, "Password not null", "Error User");
        }else if(email.length() ==0)
        {
          messages.taoTB(FacesMessage.SEVERITY_ERROR, "email not null", "Error User");
        }else
        {
           if(user.length() <6)
           {
             messages.taoTB(FacesMessage.SEVERITY_ERROR, "UserName have more than 5 characters", "Error User");
           } else if (pass.length() < 6)
           {
             messages.taoTB(FacesMessage.SEVERITY_ERROR, "Password have more than 5 characters", "Error pass");
           }else
           {
               this.users = this.usersFacade.getUserID(user);
               if(users != null)
               {
                  messages.taoTB(FacesMessage.SEVERITY_ERROR, "User already exist", "Error User");
               }else
               {
                  String password = MD5.getMD5(pass);
                  Users login1 = new Users();
                  login1.setUsername(user);
                  login1.setPassword(password);
                  login1.setEmail(email);
                  login1.setCreatedDate(createDate);
                  login1.setLastLogin(lastLogin);
                  login1.setUserStatus(1);
                  login1.setRoleID(null);
                  this.usersFacade.create(login1);
                   messages.taoTB(FacesMessage.SEVERITY_INFO, "Register Successful", "Successful");
               }
           }

        }
        }catch(Exception ex)
        {
           messages.taoTB(FacesMessage.SEVERITY_ERROR, "Error", "Error User");
        }
    }

    public Date getCreateDate() {
        return createDate;
    }

    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public Date getLastLogin() {
        return lastLogin;
    }

    public void setLastLogin(Date lastLogin) {
        this.lastLogin = lastLogin;
    }

    public String getPass() {
        return pass;
    }

    public void setPass(String pass) {
        this.pass = pass;
    }

    public int getRoleID() {
        return roleID;
    }

    public void setRoleID(int roleID) {
        this.roleID = roleID;
    }

    public String getUser() {
        return user;
    }

    public void setUser(String user) {
        this.user = user;
    }

    public int getUserStatus() {
        return userStatus;
    }

    public void setUserStatus(int userStatus) {
        this.userStatus = userStatus;
    }
    
    public Users getUsers() {
        return users;
    }

    public void setUsers(Users users) {
        this.users = users;
    }
    
}
